﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Viettel.VOFFICE.DAO.DAL.Paging;
using Viettel.VOFFICE.DAO.Models;

namespace Viettel.VOFFICE.DAO.DAL.Repository
{
    /// <summary>
    /// quản lý người dùng trong hệ thống
    /// </summary>
    public class UserRepository : IRepository<aspnet_Users>
    {

        #region Repository
        private VOFFICEContext context;

        public UserRepository(VOFFICEContext context)
        {
            // TODO: Complete member initialization
            this.context = context;
        }

        public IEnumerable<aspnet_Users> Get()
        {
            try
            {
                return context.aspnet_Users.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public aspnet_Users GetById(object id)
        {
            try
            {
                return context.aspnet_Users.Find(id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<aspnet_Users> FindByName(object s)
        {
            try
            {
                var query = (from i in context.aspnet_Users
                             where i.LoweredUserName.Equals((string)s)
                             select i);
                return query.ToList();
            }
            catch
            {
                return null;
            }
        }

        public void Insert(aspnet_Users T)
        {
            try
            {
                var existsUsers = FindByName(T.UserName.ToLower());
                if (existsUsers != null && existsUsers.Any())
                {
                    throw new DuplicateWaitObjectException();
                }
                context.aspnet_Users.Add(T);
                Save();
            }
            catch (Exception)
            {
                throw new NotSupportedException();
            }
        }

        public void Delete(object id)
        {
            var userEntity = GetById((Guid)id);
            context.aspnet_Users.Attach(userEntity);
            context.aspnet_Users.Remove(userEntity);
            Save();
        }

        public void Update(aspnet_Users T)
        {
            context.Entry(T).State = EntityState.Modified;
            Save();
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Delete(aspnet_Users T)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Extend Procedure
        public PagedList<aspnet_Users> GetWithPaging(int pageIndex, int pageSize)
        {
            return context.aspnet_Users.OrderBy(o => o.UserName).Paginate(pageIndex, pageSize);
        }

        /// <summary>
        /// Tìm kiếm người dùng theo tên
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IEnumerable<aspnet_Users> Search(string key)
        {
            return context.aspnet_Users.Where(o => o.UserName.Contains(key)).OrderBy(o => o.UserName);
        }

        /// <summary>
        /// Lấy thông tin người dùng trả về cho hàm đăng nhập
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public aspnet_Users Authen(string username)
        {
            if (string.IsNullOrWhiteSpace(username)) return null;
            return context.aspnet_Users.FirstOrDefault(o => o.LoweredUserName == username.ToLower());
        }
        #endregion

        /// <summary>
        /// Lấy toàn bộ người dùng đang hoạt động trong phòng ban
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        public IEnumerable<aspnet_Users> GetByDeptId(int depId)
        {
            return (from u in context.aspnet_Users
                    join u_in_d in context.v_UsersInDepartment on u.UserId equals u_in_d.UserId
                    where u_in_d.DeparmentId == depId && u_in_d.IsActive == true
                    select u).Distinct();
        }
    }
}
